package graph.basic;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Graph<T> {

    private Map<T, List<T>> map = new HashMap<>();

    private int edgeCount;

    public void addNewVertex(T v) {
        map.put(v, new LinkedList<>());
    }

    public void addNewEdge(T from, T to, boolean bidirectional) {

        if (!map.containsKey(from)) {
            addNewVertex(from);
        }
        if (!map.containsKey(to)) {
            addNewVertex(to);
        }

        map.get(from).add(to);

        if (bidirectional) {
            map.get(to).add(from);
        }

        edgeCount++;

    }

    public int countVertices() {
        return map.size();
    }

    public int countEdges() {
        return edgeCount;
    }

    public boolean containsVertex(T v) {
        return map.containsKey(v);
    }


    public boolean containsEdge(T from, T to) {
        if (map.containsKey(from) && map.get(from).contains(to)) {
            return true;
        } else return map.containsKey(to) && map.get(to).contains(from);
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        //foreach loop that iterates over the keys
        for (T v : map.keySet()) {
            builder.append(v.toString() + ": ");
            //foreach loop for getting the vertices
            for (T w : map.get(v)) {
                builder.append(w.toString() + " ");
            }
            builder.append("\n");
        }
        return (builder.toString());
    }



}
